package control;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import service.ItemService;
import util.Resourse;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import domain.Item;
import domain.LookedUsers;

public class ItemAction extends  ActionSupport{
	private Item item;
	private ItemService itemService;
	private File upload;
	private String uploadFileName;
	private String sellOrBuy;
	private String personl;
	private String tag;
	private String type;
	private String myS;
	private String q;
	private String ownSellOrBuy;
	private String rid;
	private String mess;
	
	
	
	public String getOwnSellOrBuy() {
		return ownSellOrBuy;
	}

	public void setOwnSellOrBuy(String ownSellOrBuy) {
		this.ownSellOrBuy = ownSellOrBuy;
	}

	public String getMyS() {
		return myS;
	}

	public void setMyS(String myS) {
		this.myS = myS;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getSellOrBuy() {
		return sellOrBuy;
	}

	public void setSellOrBuy(String sellOrBuy) {
		this.sellOrBuy = sellOrBuy;
	}

	public String getPersonl() {
		return personl;
	}

	public void setPersonl(String personl) {
		this.personl = personl;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public ItemService getItemService() {
		return itemService;
	}

	public void setItemService(ItemService itemService) {
		this.itemService = itemService;
	}

	public Item getItem() {
		return item;
	}

	public void setItem(Item item) {
		this.item = item;
	}

	public String input(){
		return SUCCESS;
	}
	public String updateItem(){
		String item_id  = null;
		Map request = (Map)	ActionContext.getContext().get("request");
		if(ServletActionContext.getRequest().getParameter("item_id")!=null)
		    item_id = (String)ServletActionContext.getRequest().getParameter("item_id");
		else{
			item_id = (String) request.get("item_id");
		}
		item =itemService.updateItem(item_id,ownSellOrBuy);
		return SUCCESS;
	}
	
	public String updateItemOperate(){
		int i = itemService.updateItemOperate(item);
		rid = String.valueOf(i);
		mess = "更新";
		return SUCCESS;
	}
	
	public String deleteItem(){
		String item_id  = null;
		Map request = (Map)	ActionContext.getContext().get("request");
		if(ServletActionContext.getRequest().getParameter("item_id")!=null)
		    item_id = (String)ServletActionContext.getRequest().getParameter("item_id");
		else{
			item_id = (String) request.get("item_id");
		}
		rid = "0";
		itemService.deleteItem(item_id);
		rid ="1";
		mess = "删除";
		return SUCCESS;
	}
	public String insert(){
//		String path ="e:/image/item"; //ServletActionContext.getServletContext().getRealPath("/images");
//		String path = ServletActionContext.getServletContext().getRealPath("/images");
		String absolutePath = Resourse.basePath+ Resourse.path+Resourse.pathItem;
		String relativepath = Resourse.path+Resourse.pathItem;
		File file = new File(absolutePath);
		if(!file.exists()){
			file.mkdir();
		}
		
		try {
			FileUtils.copyFile(upload, new File(file,uploadFileName));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Map session = ActionContext.getContext().getSession();
		String acc = (String) session.get("acc");
		itemService.insertItem(item,acc,relativepath+"/"+uploadFileName);
		return SUCCESS;
	}
	
	/*
	 * 添加物品查看人
	 */
	public String insertItemLookUser(){
//		Map request = (Map) ActionContext..get("request");
		Map session = ActionContext.getContext().getSession();
		Map request = (Map)	ActionContext.getContext().get("request");
		String acc = (String) session.get("acc");
		String item_id  = null;
		if(ServletActionContext.getRequest().getParameter("item_id")!=null)
		    item_id = (String)ServletActionContext.getRequest().getParameter("item_id");
		else{
			item_id = (String) request.get("item_id");
		}
		Item voItem =itemService.insertItemLookUser(acc, item_id);
		System.out.println(voItem);
		 List<LookedUsers> userList = voItem.getUser_look();
		
		request.put("voItem", voItem);
		request.put("userList", userList);
		return SUCCESS;
	}
	public String showOwnerItem(){
		Map session = ActionContext.getContext().getSession();
		String acc = (String) session.get("acc");
		if(ownSellOrBuy!=null){
			ownSellOrBuy = ownSellOrBuy.toLowerCase();		
		}
		session.put("temp", null);
		List list = itemService.showOwnerItem(acc,ownSellOrBuy);
		Map request = (Map)	ActionContext.getContext().get("request");
		request.put("listTemp", list);
		return SUCCESS;
	}
	public String showKeyWord(){
		List list =itemService.showKeyWord();
		Map request = (Map)	ActionContext.getContext().get("request");
		request.put("keyWords", list);
		return SUCCESS;
	}
	public String show(){
		Map session = ActionContext.getContext().getSession();
		String acc = (String) session.get("acc");
		session.put("temp", null);
//		System.out.println("成功");
//		System.out.println("我的q"+q);
		File directory = new File("");//参数为空 
		String courseFile = null;
		try {
			courseFile = directory.getCanonicalPath();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		List<Item> list = new ArrayList();
		System.out.println("当前路径"+courseFile); 
//		q = null;
//		System.out.println(sellOrBuy);
		if(q==null||q.length()==0){
		list = itemService.getAllItem(myS,acc,sellOrBuy);
		}else{
			list = itemService.queryItem(q,sellOrBuy);
		}
		Map request = (Map)	ActionContext.getContext().get("request");
		request.put("list", null);
//		System.out.println(list.get(0).getIi().getPath());
		request.put("list", list);
		return SUCCESS;
	}
	public String getTag() {
		return tag;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}

	public String getQ() {
		return q;
	}

	public void setQ(String q) {
		this.q = q;
	}

	public String getRid() {
		return rid;
	}

	public void setRid(String rid) {
		this.rid = rid;
	}

	public String getMess() {
		return mess;
	}

	public void setMess(String mess) {
		this.mess = mess;
	}
}
